$.noConflict(true);

// ----------------end of above tooltip files
/**
 * this is JQuery script
 * 
 */
// document ready handler
jQuery(function($) {
	// window.alert("Hello JQuery");
	tooltipClass = 'div.regularSection a'; // replace with your class, for
	// multiple
	// classes, separate with comma.

	function str_replace(search, replace, subject) {
		return subject.split(search).join(replace);
	}
	xOffset = 5;
	yOffset = 20;
	fadeInTime = 1000;
	jQuery(tooltipClass)
			.hover(
					function(e) {
						this.t = $(this).text();
						this.title = "";
						this.t = str_replace("::", "<br />", this.t);
						this.t = str_replace("[!]",
								"<span class='tooltipTitle'>", this.t);
						this.t = str_replace("[/!]", "</span><br />", this.t);
						this.t = str_replace("[", "<", this.t);
						this.t = str_replace("]", ">", this.t);
						if (this.t != "") {
							jQuery("body").append(
									"<p id='tooltip'>" + this.t + "</p>");
							jQuery("#tooltip").css(
									"top",
									$(this).offset().top + $(this).height()
											- xOffset + "px").css("left",
									e.screenX + "px").fadeIn(fadeInTime);
						}
					}, function() {
						jQuery("#tooltip").remove();
					});
	jQuery(tooltipClass).mousemove(
			function(e) {
				jQuery("#tooltip").css(
						"top",
						$(this).offset().top + $(this).height() - xOffset
								+ "px").css("left", e.screenX + "px");
			});

	// $('.commandButtonOrLink')

});

function HandleAjaxError(xhr, status, exception) {
	var message = "There was an error with the AJAX request.\n";
	switch (status) {
	case 'timeout':
		message += "The request timed out.";
		break;
	case 'notmodified':
		message += "The request was not modified but was not retrieved from the cache.";
		break;
	case 'parseerror':
		message += "XML/Json format is bad.";
		break;
	default:
		message += "HTTP Error (" + request.status + " " + request.statusText
				+ ").";
	}
	if (exception != null) {
		message += "Exception has encountered: " + exception;
	}
	message += "\n";
	alert(message);
}

// used to save
function ajaxSave(source) {
	var ed = tinyMCE.get('textarea');

	// Do you ajax call here, window.setTimeout fakes ajax call
	ed.setProgressState(1); // Show progress
	alert(ed.getContent());
	/*
	 * document.getElementById('textareahiddenid').setValue(ed.getContent());
	 * jsf.ajax.request(source, event, { render : '@form', exectute : '@form'
	 * }); window.setTimeout(function() { ed.setProgressState(0); // Hide
	 * progress alert(ed.getContent()); }, 300);
	 */
}

function handleComplete(xhr, status, args) {
	var isValid = args.isValid;
	if (isValid) {
		if (typeof messageDlg != 'undefined') {
			messageDlg.hide();
		}
		if (typeof loginDlg != 'undefined') {
			loginDlg.hide();
			// init textarea with xheditor when user log in on viewthread page
			// window.xheditor = false;
			// initTextAreaWithEditor();
			window.location = location.href;
		}
	}
}

// move cursor to the given location. used in the viewThread page, when reply
// button is clicked.
function jumpTo(to) {
	var new_position = jQuery(to).offset();
	window.scrollTo(new_position.left, new_position.top);
}

// seach the 1st xheditor from the given fromForm for the PIcture URL and set it
// to target value
function searchAndSetThumbPicURL(fromFormId, to) {
	var picURL = $(fromFormId).find('td.xheIframeArea').find('iframe').first()
			.contents().find('body').find('img').first().attr('src');
	// alert(picURL);

	if (picURL) {
		$(to).attr('value', picURL);
		// alert($(to).attr('value'));
	}
}
